package com.bjpowernode.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.json.JSONUtil;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.entity.TCustomerRemark;
import com.bjpowernode.entity.TUser;
import com.bjpowernode.mapper.TCustomerRemarkMapper;
import com.bjpowernode.query.TCustomerRemarkQuery;
import com.bjpowernode.service.TCustomerRemarkService;
import com.bjpowernode.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class TCustomerRemarkServiceImpl implements TCustomerRemarkService {

    @Resource
    private TCustomerRemarkMapper tCustomerRemarkMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public int saveRemark(TCustomerRemarkQuery tCustomerRemarkQuery) {
        // 创建对象
        TCustomerRemark tCustomerRemark = new TCustomerRemark();
        // 赋值
        BeanUtils.copyProperties(tCustomerRemarkQuery, tCustomerRemark);
        // 设置
        tCustomerRemark.setCreateTime(DateTime.now());
        tCustomerRemark.setEditTime(DateTime.now());
        // 解析
        String parseToken = jwtUtil.parseToken(tCustomerRemarkQuery.getJwt());
        // json转user
        TUser tUser = JSONUtil.toBean(parseToken, TUser.class);
        // 赋值
        tCustomerRemark.setCreateBy(tUser.getId());
        tCustomerRemark.setEditBy(tUser.getId());
        int i = tCustomerRemarkMapper.insertSelective(tCustomerRemark);
        return i;
    }

    @Override
    public PageInfo<TCustomerRemark> loadCustomerHistoryRemark(Integer customerId, Integer current) {
        // 开启分页
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 查询
        List<TCustomerRemark> tCustomerRemarkList = tCustomerRemarkMapper.selectByCustomerId(customerId);
        // 封装
        PageInfo<TCustomerRemark> pageInfo = new PageInfo<>(tCustomerRemarkList);
        // 返回
        return pageInfo;
    }

    @Override
    public int removeById(Integer id) {
        // 起始是更新操作
        TCustomerRemark tCustomerRemark = new TCustomerRemark();
        tCustomerRemark.setId(id);
        tCustomerRemark.setDeleted(1);
        tCustomerRemark.setEditTime(DateTime.now());
        int i = tCustomerRemarkMapper.updateByPrimaryKeySelective(tCustomerRemark);
        return i;
    }

    @Override
    public TCustomerRemark loadCustomerRemark(Integer id) {
        TCustomerRemark tCustomerRemark = tCustomerRemarkMapper.selectByPrimaryKey(id);
        return tCustomerRemark;
    }

    @Override
    public int updateRemarkById(TCustomerRemarkQuery tCustomerRemarkQuery) {
        // 创建对象
        TCustomerRemark tCustomerRemark = new TCustomerRemark();
        // 赋值替换
        BeanUtils.copyProperties(tCustomerRemarkQuery, tCustomerRemark);
        // 设置编辑人
        String parseToken = jwtUtil.parseToken(tCustomerRemarkQuery.getJwt());
        TUser tUser = JSONUtil.toBean(parseToken, TUser.class);
        tCustomerRemark.setEditBy(tUser.getId());
        tCustomerRemark.setEditTime(DateTime.now());
        int i = tCustomerRemarkMapper.updateByPrimaryKeySelective(tCustomerRemark);
        return i;
    }
}
